/**
 * @Description: 菜单配置
 * @Copyright: 2017 www.fallsea.com Inc. All rights reserved.
 * @author: fallsea
 * @version 1.7.0
 * @License：MIT
 */
layui.define(['element', "fsConfig", "fsCommon"], function (exports) {

  var menuConfig = {
    dataType: "local", //获取数据方式，local本地获取，server 服务端获取
    loadUrl: "", //加载数据地址
    rootMenuId: "0", //根目录菜单id
    defaultSelectTopMenuId: "main", //默认选中头部菜单id
    defaultSelectLeftMenuId: "dashboard", //默认选中左边菜单id
    menuIdField: "menuId", //菜单id
    menuNameField: "menuName", //菜单名称
    menuIconField: "menuIcon", //菜单图标，图标必须用css
    menuHrefField: "menuHref", //菜单链接
    parentMenuIdField: "parentId",//父菜单id
    data: [
      // 有且只能有一个0级菜单 否则后果自负
      {"menuId": "main", "menuName": "控制台", "menuIcon": "fa-cog", "menuHref": "", "parentId": "0"},
      // 一级菜单
      {
        "menuId": "dashboard",
        "menuName": "Dashboard",
        "menuIcon": "icon-dashboard",
        "menuHref": "",
        "parentId": "main"
      }, {
        "menuId": "forms",
        "menuName": "表单页",
        "menuIcon": "icon-form",
        "menuHref": "",
        "parentId": "main"
      }, {
        "menuId": "lists",
        "menuName": "列表页",
        "menuIcon": "icon-table",
        "menuHref": "",
        "parentId": "main"
      }, {
        "menuId": "details",
        "menuName": "详情页",
        "menuIcon": "icon-profile",
        "menuHref": "",
        "parentId": "main"
      }, {
        "menuId": "results",
        "menuName": "结果页",
        "menuIcon": "icon-checkcircle",
        "menuHref": "",
        "parentId": "main"
      }, {
        "menuId": "iconfonts",
        "menuName": "字体图标",
        "menuIcon": "icon-appstore1",
        "menuHref": "pages/iconFonts/demo_fontclass.html",
        "parentId": "main"
      },
      // 二级菜单
      // 表单
      {
        "menuId": "basicForm",
        "menuName": "基础表单",
        "menuIcon": "",
        "menuHref": "pages/forms/basicForm.html",
        "parentId": "forms"
      }, {
        "menuId": "stepForm",
        "menuName": "分步表单",
        "menuIcon": "",
        "menuHref": "pages/forms/stepsForm.html",
        "parentId": "forms"
      },
      // 列表
      {
        "menuId": "basicList",
        "menuName": "基础列表",
        "menuIcon": "",
        "menuHref": "pages/lists/basicList.html",
        "parentId": "lists"
      }, {
        "menuId": "queryList",
        "menuName": "查询列表",
        "menuIcon": "",
        "menuHref": "pages/lists/queryList.html",
        "parentId": "lists"
      },
      // 详情
      {
        "menuId": "basicDetails",
        "menuName": "基础详情页",
        "menuIcon": "",
        "menuHref": "pages/details/basicDetail.html",
        "parentId": "details"
      }, {
        "menuId": "advanceDetails",
        "menuName": "高级详情页",
        "menuIcon": "",
        "menuHref": "pages/details/advanceDetail.html",
        "parentId": "details"
      },
      // 结果页
      {
        "menuId": "resultSuccess",
        "menuName": "成功页",
        "menuIcon": "",
        "menuHref": "pages/results/resultSuccess.html",
        "parentId": "results"
      }, {
        "menuId": "resultError",
        "menuName": "失败页",
        "menuIcon": "",
        "menuHref": "pages/results/resultError.html",
        "parentId": "results"
      },
    ]
  };

  var element = layui.element,
    fsCommon = layui.fsCommon,
    fsConfig = layui.fsConfig,
    statusName = $.result(fsConfig, "global.result.statusName", "errorNo"),
    msgName = $.result(fsConfig, "global.result.msgName", "errorInfo"),
    dataName = $.result(fsConfig, "global.result.dataName", "results.data"),
    FsMenu = function () {

    };


  FsMenu.prototype.render = function () {

    this.loadData();

    this.showMenu();
  };

  /**
   * 加载数据
   */
  FsMenu.prototype.loadData = function () {

    if (menuConfig.dataType == "server") {//服务端拉取数据

      var url = menuConfig.loadUrl;
      if ($.isEmpty(url)) {
        fsCommon.errorMsg("未配置请求地址！");
        return;
      }

      fsCommon.invoke(url, {}, function (data) {
        if (data[statusName] == "0") {
          menuConfig.data = $.result(data, dataName);
        }
        else {
          //提示错误消息
          fsCommon.errorMsg(data[msgName]);
        }
      }, false);

    }

  }


  /**
   * 获取图标
   */
  FsMenu.prototype.getIcon = function (menuIcon) {

    if (!$.isEmpty(menuIcon)) {

      if (menuIcon.indexOf("<i") == 0) {
        return menuIcon;
      } else if (menuIcon.indexOf("icon-" == 0)) {
        return '<i class="anticon ' + menuIcon + '"></i>';
      } else if (menuIcon.indexOf("&#") == 0) {
        return '<i class="layui-icon">' + menuIcon + '</i>';
      } else if (menuIcon.indexOf("fa-") == 0) {
        return '<i class="fa ' + menuIcon + '"></i>';
      } else {
        return '<i class="' + menuIcon + '"></i>';
      }
    }
    return '<i class="anticon icon-appstore-o"></i>';
  };

  /**
   * 清空菜单
   */
  FsMenu.prototype.cleanMenu = function () {
    $("#fsTopMenu").html("");
    $("#fsLeftMenu").html("");
  }
  /**
   * 显示菜单
   */
  FsMenu.prototype.showMenu = function () {
    var thisMenu = this;
    var data = menuConfig.data;
    if (!$.isEmpty(data)) {
      var _index = 0;
      //显示顶部一级菜单
      var fsTopMenuElem = $("#fsTopMenu");
      var fsLeftMenu = $("#fsLeftMenu");
      fsLeftMenu.append("<div class=\"fsSwitchMenu layout-switch-menu\" >" +
        "<i class=\"anticon icon-menu-unfold\" style=\" vertical-align: middle;\"></i>" +
        "</div>")
      $.each(data, function (i, v) {
        if (menuConfig.rootMenuId === v[menuConfig.parentMenuIdField]) {

          var topStr = '<li class="layui-nav-item';
          if ($.isEmpty(menuConfig.defaultSelectTopMenuId) && _index === 0) {//为空默认选中第一个
            topStr += ' layui-this';
          } else if (!$.isEmpty(menuConfig.defaultSelectTopMenuId) && menuConfig.defaultSelectTopMenuId == v[menuConfig.menuIdField]) {//默认选中处理
            topStr += ' layui-this';
          }
          _index++;
          topStr += '" dataPid="' + v[menuConfig.menuIdField] + '"><a href="javascript:;">' + thisMenu.getIcon(v[menuConfig.menuIconField]) + ' <cite>' + v[menuConfig.menuNameField] + '</cite></a></li>';
          fsTopMenuElem.append(topStr);

          //显示二级菜单，循环判断是否有子栏目
          $.each(data, function (i2, v2) {
            if (v[menuConfig.menuIdField] === v2[menuConfig.parentMenuIdField]) {

              var menuRow = '<li class="layui-nav-item';
              if (!$.isEmpty(menuConfig.defaultSelectLeftMenuId) && menuConfig.defaultSelectLeftMenuId == v2[menuConfig.menuIdField]) {//默认选中处理
                menuRow += ' layui-this';
              }
              //显示三级菜单，循环判断是否有子栏目
              var menuRow3 = "";
              $.each(data, function (i3, v3) {
                if (v2[menuConfig.menuIdField] === v3[menuConfig.parentMenuIdField]) {
                  if ($.isEmpty(menuRow3)) {
                    menuRow3 = '<dl class="layui-nav-child">';
                  }
                  menuRow3 += '<dd';
                  if (!$.isEmpty(menuConfig.defaultSelectLeftMenuId) && menuConfig.defaultSelectLeftMenuId == v3[menuConfig.menuIdField]) {//默认选中处理
                    menuRow3 += ' class="layui-this"';
                    menuRow += ' layui-nav-itemed';//默认展开二级菜单
                  }

                  menuRow3 += ' id="' + v3[menuConfig.menuIdField] + '" lay-id="' + v3[menuConfig.menuIdField] + '"><a href="javascript:;" menuId="' + v3[menuConfig.menuIdField] + '" dataUrl="' + v3[menuConfig.menuHrefField] + '">' + thisMenu.getIcon(v3[menuConfig.menuIconField]) + ' <cite>' + v3[menuConfig.menuNameField] + '</cite></a></dd>';

                }

              });

              menuRow += '" id="' + v2[menuConfig.menuIdField] + '" lay-id="' + v2[menuConfig.menuIdField] + '" dataPid="' + v2[menuConfig.parentMenuIdField] + '" style="display: none;"><a href="javascript:;" menuId="' + v2[menuConfig.menuIdField] + '" dataUrl="' + v2[menuConfig.menuHrefField] + '">' + thisMenu.getIcon(v2[menuConfig.menuIconField]) + ' <cite>' + v2[menuConfig.menuNameField] + '</cite></a>';


              if (!$.isEmpty(menuRow3)) {
                menuRow3 += '</dl>';

                menuRow += menuRow3;
              }

              menuRow += '</li>';

              fsLeftMenu.append(menuRow);
            }

          });

        }
      });
    }
    element.render("nav");
    $(".layui-nav-item").on('hover', function () {
      console.log(1)
    })
  };

  var fsMenu = new FsMenu();

  exports("fsMenu", fsMenu);
});

